package org.bonorsoft.modules.control.service.impl;

import org.bonorsoft.common.constant.CacheConstant;
import org.bonorsoft.common.constant.CommonConstant;
import org.bonorsoft.common.util.StringUtils;
import org.bonorsoft.modules.control.entity.TreeData;
import org.bonorsoft.modules.control.mapper.InforMapper;
import org.bonorsoft.modules.control.service.IInforService;
import org.bonorsoft.modules.control.vo.Otree;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class InforServiceImpl implements IInforService {

    @Resource
    private InforMapper inforMapper;


    /**
     * 获取区划信息
     *
     * @param req
     * @param parent
     * @return
     */
    @Override
    @Cacheable(value = CacheConstant.OFFICE_TREE_DATA, key = "#parent.deptName")
    public List<Otree> selectTreeData(HttpServletRequest req, TreeData parent) {
        ArrayList<TreeData> treeDataArrayList = new ArrayList<>();
        treeDataArrayList.add(new TreeData().setDeptId(CommonConstant.YONG_AN_PARENT_ID).setParentId("0").setDeptName("垦利县公安局永安派出所"));
        List<TreeData> treeData = inforMapper.selectTreeData(CommonConstant.YONG_AN_PARENT_ID, req);
        //获取乡镇信息
        List<TreeData> childData = getChildData(treeData);
        //获取地址信息
        List<TreeData> childrenData = getChildrenData(childData);
        treeDataArrayList.addAll(treeData);
        treeDataArrayList.addAll(childData);
        treeDataArrayList.addAll(childrenData);

        List<Otree> ztrees = initZtree(treeDataArrayList);
        return ztrees;
    }

    /**
     * 一级
     *
     * @param req
     * @return
     */
    @Override
    public List<Otree> selectTree(HttpServletRequest req) {
        ArrayList<TreeData> treeDataArrayList = new ArrayList<>();
        List<TreeData> treeData = inforMapper.selectTreeData(CommonConstant.YONG_AN_PARENT_ID, req);
        treeDataArrayList.addAll(treeData);
        List<Otree> ztrees = initZtree(treeDataArrayList);
        return ztrees;
    }

    /**
     * 二级
     *
     * @param id
     * @return
     */
    @Override
    public List<Otree> getChildTree(String id) {
        String[] strings = new String[]{id};
        ArrayList<TreeData> treeDataArrayList = new ArrayList<>();
        List<TreeData> childData = inforMapper.selectChildData(strings);
        treeDataArrayList.addAll(childData);
        List<Otree> ztrees = initZtree(treeDataArrayList);
        return ztrees;
    }

    /**
     * 三级
     * @param id
     * @return
     */
    @Override
    public List<Otree> getChildrenTree(String id) {
        String[] strings = new String[]{id};
        ArrayList<TreeData> treeDataArrayList = new ArrayList<>();
        List<TreeData> childrenData = inforMapper.selectChildrenData(strings);
        treeDataArrayList.addAll(childrenData);
        List<Otree> ztrees = initZtree(treeDataArrayList);
        return ztrees;
    }

    /**
     * 获取乡镇信息
     *
     * @param treeData
     * @return
     */
    private List<TreeData> getChildData(List<TreeData> treeData) {
        final String[] parentIds = {""};
        treeData.forEach(item -> parentIds[0] += item.getDeptId() + ",");
        String pIds = parentIds[0].substring(0, parentIds[0].length() - 1);
        return inforMapper.selectChildData(pIds.split(","));
    }

    /**
     * 获取地址信息
     *
     * @param childData
     * @return
     */
    private List<TreeData> getChildrenData(List<TreeData> childData) {
        final String[] parentIds = {""};
        childData.forEach(item -> parentIds[0] += item.getDeptId() + ",");
        String pIds = parentIds[0].substring(0, parentIds[0].length() - 1);
        return inforMapper.selectChildrenData(pIds.split(","));
    }

    /**
     * 对象转部门树
     *
     * @param treeData 部门列表
     * @return 树结构列表
     */
    public List<Otree> initZtree(List<TreeData> treeData) {

        return initZtree(treeData, null);
    }

    /**
     * 对象转部门树
     *
     * @param treeData     部门列表
     * @param roleDeptList 角色已存在菜单列表
     * @return 树结构列表
     */
    public List<Otree> initZtree(List<TreeData> treeData, List<String> roleDeptList) {

        List<Otree> otrees = new ArrayList<Otree>();
        boolean isCheck = StringUtils.isNotNull(roleDeptList);
        for (TreeData data : treeData) {

            Otree otree = new Otree();
            otree.setId(data.getDeptId());
            otree.setpId(data.getParentId());
            otree.setName(data.getDeptName());
            otree.setTitle(data.getDeptName());
            otree.setRemarks(data.getRemarks());
            if (isCheck) {
                otree.setChecked(roleDeptList.contains(data.getDeptId() + data.getDeptName()));
            }
            otrees.add(otree);

        }
        return otrees;
    }
}
